package fun.ticsmyc.sort;

import fun.ticsmyc.tools.ArrayTools;
import org.junit.Test;

import java.util.Arrays;

/**
 * 简单选择排序
 *
 * @author Ticsmyc
 * @package fun.ticsmyc.sort1
 * @date 2020-01-30 17:26
 */
public class SelectSort {

    public static void selectSort(int[] arr ){
        for(int sortedIndex= 0 ; sortedIndex < arr.length ; sortedIndex++){

            int min=arr[sortedIndex];
            int minIndex = sortedIndex;

            for(int i=sortedIndex+1;i<arr.length;++i){
                if(arr[i] < min){
                    min=arr[i];
                    minIndex=i;
                }
            }
            ArrayTools.swap(arr,sortedIndex,minIndex);
//            ArrayTools.printArray(arr);
        }

    }


    @Test
    public void test(){

        //测试次数
        int testTime=5_000_000;
        //数组大小
        int size=10;
        //数组值 -100 ~ +100
        int value=100;
        boolean succeed =true;

        for(int i=0;i<testTime;++i){
            int[] arr1 = ArrayTools.generateRandomArray(size,value);
            int[] arr2 = Arrays.copyOf(arr1,arr1.length);
            int[] arr3 = Arrays.copyOf(arr1,arr1.length);

            selectSort(arr1);
            ArrayTools.rightSortMethod(arr2);
            if(!Arrays.equals(arr1,arr2)){
                succeed=false;
                //打印原数组
                ArrayTools.printArray(arr3);
                break;
            }
        }
        System.out.println( succeed ? "nice" : "Fucking fucked!");
    }

    @Test
    public void testOne(){
        int[] arr1 =new int[]{-82,32  ,-3 , -17 , -14 , 43  ,71,  40 , 35 , 24 };
        ArrayTools.printArray(arr1);
        selectSort(arr1);

    }

}
